/*
 * $Id: $
 */
package org.rcgwt.client.ui.grid.provider;

import org.rcgwt.client.ui.grid.event.FocusElementEvent;
import org.rcgwt.client.ui.grid.event.FocusElementHandler;
import org.rcgwt.client.ui.grid.internal.HandlerManagerProvider;
import org.rcgwt.client.ui.grid.internal.model.IElementModel;
import org.rcgwt.client.ui.grid.internal.model.IViewModel;
import org.rcgwt.client.ui.grid.model.IGridWidget;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.HandlerRegistration;

/**
 * 
 * @author Olivier Oeuillot (latest modification by $Author: $)
 * @version $Revision: $ $Date: $
 */
public class BasicFocusProvider extends HandlerManagerProvider implements
        IFocusProvider, IGridWidgetProvider {

    private boolean hasFocus;

    private IElementModel currentFocusElementModel;

    private IViewModel currentFocusViewModel;

    private IGridWidget gridWidget;

    @Override
    public IElementModel getFocusElementModel() {
        return currentFocusElementModel;
    }

    @Override
    public void setFocusElement(IViewModel newViewModel,
            IElementModel newElementModel) {

        if (currentFocusElementModel == newElementModel
                && currentFocusElementModel == newViewModel) {
            return;
        }

        IElementModel oldElementModel = currentFocusElementModel;
        IViewModel oldViewModel = currentFocusViewModel;

        currentFocusElementModel = newElementModel;
        currentFocusViewModel = newViewModel;

        Log.debug("Set focusElement=" + currentFocusElementModel
                + " viewElement=" + currentFocusViewModel + " hasFocus="
                + hasFocus);

        if (hasFocus == false) {
            return;
        }

        FocusElementEvent.fire(this, oldElementModel, currentFocusElementModel,
                oldViewModel, currentFocusViewModel);
    }

    @Override
    public IViewModel getFocusViewModel() {
        return currentFocusViewModel;
    }

    @Override
    public HandlerRegistration addFocusElementHandler(
            FocusElementHandler<IElementModel> handler) {
        return ensureHandlers()
                .addHandler(FocusElementEvent.getType(), handler);
    }

    @Override
    public void install(IGridWidget gridWidget) {
        this.gridWidget = gridWidget;
    }

    @Override
    public void uninstall(IGridWidget gridWidget) {

    }

    @Override
    public void getFocus() {
        if (hasFocus) {
            return;
        }
        hasFocus = true;

        FocusElementEvent.fire(this, null, currentFocusElementModel, null,
                currentFocusViewModel);
    }

    @Override
    public void looseFocus() {
        if (hasFocus == false) {
            return;
        }
        hasFocus = false;

        FocusElementEvent.fire(this, currentFocusElementModel, null,
                currentFocusViewModel, null);
    }

    public boolean hasFocus() {
        return hasFocus;
    }
}
